package cn.tm.liu.mvc.controller;

import cn.tm.liu.mvc.common.HttpResult;
import cn.tm.liu.mvc.common.ResponseResult;
import cn.tm.liu.mvc.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author Marvin.L
 * @description
 * @date 2022.03.02 14.06
 */
@Slf4j
@RestController
@ResponseResult
public class TestController {

    @RequestMapping(value = "/404", method = RequestMethod.GET)
    public HttpResult notFound() {
        return HttpResult.failure("Not Found").extra();
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String hello() {
        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attr.getRequest();
        log.debug(request.getSession().getId());
        return "Hello Springboot";
    }

    @RequestMapping(value = "/send", method = RequestMethod.GET)
    public void receive(@RequestParam String s) {
        log.info(s);
    }

    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public List all() {
        List<Object> objects = Arrays.asList(
                new Order().setId(3).setCustomer(5).setAmount(500).setProduct("X-X-1"),
                new Order().setId(4).setCustomer(5).setAmount(900).setProduct("X-X-2"),
                new Order().setId(5).setCustomer(6).setAmount(600).setProduct("X-X-3"),
                new Order().setId(7).setCustomer(6).setAmount(500).setProduct("X-X-2")
        );
        return objects;
    }


}
